<?php

namespace app\admin\controller\staff;

use app\admin\model\Staff as StaffModel;
use app\admin\model\Token;
use app\admin\service\ServiceTime;
use app\common\Common;
use app\sys\service\AliOss;
use app\sys\service\UserApi;
use think\Db;
use think\Exception;
use think\facade\Cache;
use think\facade\Env;
use think\Request;
use think\facade\Validate;

class Staff extends Common
{
    public function login(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "user|账号" => "require",
            "password|密码" => "require",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        list($status,$msg) = StaffModel::login($data["user"],$data["password"],$request->host());
        if ($status == false){
            // 验证
           return failedJson($msg);
        }

        $request->_user = "";
        $resData = array();
        // 启动事务-
        Db::startTrans();
        try {
            $res = StaffModel::add($msg);
            if (!$res) {
                Db::rollback();
                return failedJson("登录失败");
            }

            $resData["id"] = $res["id"];
            $resData["staff_no"] = $res["staff_no"];
            $resData["staff_name"] = $res["staff_name"];
            $resData["mobile"] = $res["mobile"];
            $resData["token"] = $msg["Token"];
            $resData["expire_time"] = strtotime($msg["ExpireTime"]);
            $resData["icon"] = $res["icon"];
            $resData["sex"] = $res["sex"];
            $token = Token::SaveToken($msg);
            if (!$token) {
                Db::rollback();
                return failedJson("登录失败");
            }
            $request->_user = $res;
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return failedJson("登录失败",1,$e->getMessage());
        }
        return successJson("success",$resData);
    }

    // 获取用户信息
    public function info(Request $request)
    {
        $info = $request->_user;
        return successJson("success",$info);
    }

    // 信息上传
    public function updateInfo(Request $request)
    {
        $imgOb = $request->file("img");

        $sex = $request->param("sex","");
        $name = $request->param("name","");

        if($imgOb && !$imgOb->checkSize(2097152)) {
            return failedJson("图片不能大于2M~");
        }
        if($imgOb && !$imgOb->checkImg()){
            return failedJson("只能上传图片");
        }

        $info = $request->_user;
        $staffInfo = \app\admin\model\Staff::field("icon,sex")->where("staff_no",$info["staff_no"])->find();
        if(!$staffInfo){
            return failedJson("客服信息不存在~");
        }
        $div = Env::get("ALIOSS.OSS_DIV");

        $path = $div."/".$info["staff_no"].".jpg";

        $arr = ["sex"=>$sex,"name"=>$name];
        if($imgOb){
            $im =$imgOb->getInfo();
            $a = new AliOss();
            if(!empty($staffInfo["icon"]))
                $a->oss_delete_file($staffInfo["icon"]);
            $a->oss_upload_content($path,$im["tmp_name"]);
            $arr = ["icon"=>$path,"sex"=>$sex,"name"=>$name];
        }
        $staffInfo->data($arr)->save();
        return successJson("success");
    }

    // 登出
    public function loginOut(Request $request)
    {
        $res = $request->_user;
        try {
            Token::where("token", \think\facade\Request::header("token"))->delete();

        } catch (Exception $e) {
            return failedJson("退出失败.", 1, $e->getMessage());
        }

        return successJson("退出成功.");
    }


}